﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Esint.JudicialAuction.Model;
using Esint.JudicialAuction.Factory;
using Esint.JudicialAuction.IDAL;
using Esint.Common.Model;
using System.Linq;
using System.Diagnostics;

namespace Esint.JudicialAuction.BLL
{
    /// <summary>
    /// 文件说明:  业务逻辑层
    /// 作    者: 谷明
    /// 生成日期: 2020年03月29日
    /// 模板版本: Esint.Template.BLL.BLL_01 版，适用于简单三层开发!
    /// 功能说明：
    /// </summary>
    public partial class Schedule_UserService
    {
        /// <summary>
        /// 功    能: 保存对象
        /// 说    明: 当主键为空时，新增操作
        ///           当主键不为空，根据主键进行修改
        /// <summary>
        public void Save(Schedule_UserInfo schedule_User)
        {
            if (String.IsNullOrEmpty(schedule_User.UserId))
            {
                schedule_User.UserId = Guid.NewGuid().ToString();
                schedule_User.Priority = 0;
                schedule_User.ScheduleWeight = GetAllUserAverageWeight();
                dataAccess.Insert(schedule_User);
            }
            else
            {
                dataAccess.Update(schedule_User);
            }
        }

        /// <summary>
        /// 获得所有参与排班的用户的权重平均值
        /// </summary>
        /// <returns></returns>
        public int GetAllUserAverageWeight()
        {
            return dataAccess.GetAllUserAverageWeight();
        }

        /// <summary>
        /// 获得当前排班值不是最大的，并且符合不连班需求的所有人方法（用于顺序排班的第二天排白/夜班）
        /// </summary>
        /// <param name="preDate">前一天的日期</param>
        /// <returns></returns>
        public List<Schedule_UserInfo> GetCanBeAutoList(string currentDate, int workType)
        {
            var list = dataAccess.GetCanBeAutoAndMinestTimesList(currentDate, workType);

            //var minValue = list.Min(x => x.Priority);

            //var minList = list.FindAll(x => x.Priority == minValue);

            //if (minList.Count < 8)
            //{
            //    var takeNum = 8 - minList.Count;

            //    var qy = from t in list where t.Priority!=null && t.Priority.Value != minValue orderby t.Priority ascending select t;

            //    var addedList = qy.ToList().Take(takeNum);
            //    minList.AddRange(addedList);
            //}

            //Debug.WriteLine($"日期：{currentDate}；一共可用：{list.Count}人；取出可用：{minList.Count}人");

            //return minList;
            return list;
        }

        public List<Schedule_UserInfo> GetAllUserList(string preDate, int workType)
        {
            return dataAccess.GetAllUserList();
        }

        /// <summary>
        /// 根据查询条件获得所有排班人员分页集合对象
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="isAvailable"></param>
        /// <returns></returns>
        public PagedList<Schedule_UserInfo> GetSchedulUserList(string userName, string isAvailable, string gender, string pageIndex, string pageSize)
        {

            byte? IsAvailable = null;
            {
                if (byte.TryParse(isAvailable, out byte n))
                {
                    IsAvailable = n;
                }
            }

            int? Gender = null;
            {
                if (int.TryParse(gender, out int n))
                {
                    Gender = n;
                }
            }
            
            return dataAccess.GetSchedulUserList(userName, IsAvailable, Gender, int.Parse(pageIndex), int.Parse(pageSize));
        }


        /// <summary>
        /// 保存列表方法
        /// </summary>
        /// <param name="list"></param>
        public void SaveMutli(List<Schedule_UserInfo> list)
        {
            dataAccess.SaveMutli(list);
        }

        /// <summary>
        /// 获得所有用户列表
        /// </summary>
        /// <returns></returns>
        public List<Schedule_UserInfo> GetAllUserList()
        {
            return dataAccess.GetAllUserList();
        }

        /// <summary>
        /// 根据id集合获得用户列表
        /// </summary>
        /// <param name="userIdList"></param>
        /// <returns></returns>
        public List<Schedule_UserInfo> GetUserListByIdList(List<string> userIdList)
        {
            return dataAccess.GetUserListByIdList(userIdList);
        }

        /// <summary>
        /// 获得人员比
        /// </summary>
        /// <returns></returns>
        public double[] GetAvailableUserGenderNum()
        {
            return dataAccess.GetAvailableUserGenderNum();
        }
    }
}
